﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class yourPage : System.Web.UI.Page
{
    CommonPage cm4 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["currentItem"] = "yoursp";
        if (!IsPostBack)
        {
            Page.Title = "Vieclamgap.net | Trang cá nhân | Quản lý các hoạt đông của bạn trên Vieclamgap.net";
            try
            {
                if (String.IsNullOrEmpty(Session["KhachHang"].ToString()))
                {
                    Response.Redirect(ResolveClientUrl("warnLogin.aspx"));
                }
                else
                {
                    fillDulieuLenGrid();
                }
            }
            catch { Response.Redirect(ResolveClientUrl("Default.aspx")); }
        }
    }

    void fillDulieuLenGrid()
    {
        int idUs = cm4.Customer.idAcc;
        var gigCanhan = from gg2 in _db8.Gigs
                        where gg2.idAcc == idUs && gg2.TrangThai == (int)EnumGigStatus.GigHien && gg2.Duyet==true
                        orderby gg2.NgayTao
                        select gg2;
        //DataList1.DataSource = gigCanhan;
        //DataList1.DataBind();
        GridView1.DataSource = gigCanhan;
        GridView1.DataBind();
        
        // Cac cong viec duoc duyet roi nhung vi pham
        var gigCanhanDel = from gg2 in _db8.Gigs
                        where gg2.idAcc == idUs && gg2.TrangThai == (int)EnumGigStatus.GigKhongHopLe&& gg2.Duyet==true
                        orderby gg2.NgayTao
                        select gg2;
        GridView2.DataSource = gigCanhanDel;
        GridView2.DataBind();
        // Cac cong viec chua dc duyet
        var gigCanhanNot = from gg2 in _db8.Gigs
                           where gg2.idAcc == idUs && gg2.Duyet==false
                           orderby gg2.NgayTao
                           select gg2;
        GridView3.DataSource = gigCanhanNot;
        GridView3.DataBind();
        // Cac cong viec DEACTIVE
        var gigCanhanDeactive = from gg24 in _db8.Gigs
                           where gg24.idAcc == idUs && gg24.TrangThai==(int)EnumGigStatus.GigAn && gg24.Duyet == true
                           orderby gg24.NgayTao
                           select gg24;
        GridView4.DataSource = gigCanhanDeactive;
        GridView4.DataBind();
        
    }
    protected void linkAdd_Click(object sender, EventArgs e)
    {

        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add"));
    }
    protected void linkDelG_Click(object sender, EventArgs e)
    {
        try
        {
            string lDel = ((LinkButton)sender).CommandArgument.ToString();
            string tenhinh = "";
            Gig giDel = _db8.Gigs.Single(iiiid => iiiid.idGig == int.Parse(lDel));
            tenhinh = giDel.UrlHinh1;
            _db8.Gigs.DeleteOnSubmit(giDel);
            _db8.SubmitChanges();

            // Xoa hinh tren host
            string urlIMG = Server.MapPath("~/upload/")+tenhinh;
            string urlIMGThum = Server.MapPath("~/upload/thumnails/") + tenhinh;
            File.Delete(urlIMG);
            File.Delete(urlIMGThum);
            this.fillDulieuLenGrid();
        }
        catch {
            this.fillDulieuLenGrid();
            Literal1.Text = "<script type=\"text/javascript\">" +
                                "$(function() { " +
                                "alert(\"Không thể xóa công việc này. Vì đang có đơn đặt hàng.\");" +
                                "});" +
                                "</script>";
        }
    }
    protected void linkEditG_Click(object sender, EventArgs e)
    {
        Literal1.Text = "<script>alert('Hello')</script>";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btnTiep2_Click(object sender, EventArgs e)
    {
        string noidung = TextBox1.Text.ToString().Trim();
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add&to=") + noidung);
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void linkActive_Click(object sender, EventArgs e)
    {
        LinkButton linkac = (LinkButton)GridView1.FindControl("linkActive");
      
    }
    protected void linkActive_Command(Object sender, CommandEventArgs e)
    {
       activeGig(e.CommandArgument.ToString());
    }
    
    protected void linkDeactive_Command(Object sender, CommandEventArgs e)
    {
       deactivegig(e.CommandArgument.ToString());
    }

    // 2 hàm de Active va DeActive
    void activeGig(string idgig)
    {
        Gig gactive = _db8.Gigs.Single(vis => vis.idGig == int.Parse(idgig));
        if (gactive.TrangThai == (int)EnumGigStatus.GigHien)
        {
            return;
            this.fillDulieuLenGrid();
        }
        else
        {
            gactive.TrangThai = (int)EnumGigStatus.GigHien;
            _db8.SubmitChanges();

            this.fillDulieuLenGrid();
        }
    }
    void deactivegig(string idG)
    {
        Gig gactive = _db8.Gigs.Single(vis => vis.idGig == int.Parse(idG));
        if (gactive.TrangThai == (int)EnumGigStatus.GigAn)
        {
            return;
            this.fillDulieuLenGrid();
        }
        else
        {
            gactive.TrangThai = (int)EnumGigStatus.GigAn;
            _db8.SubmitChanges();
            this.fillDulieuLenGrid();
        }
    }
}
